<template>
  <div class="article-container">
    <el-card class="box-card"
             v-loading="loading">
      <div slot="header"
           class="head-box">
        <span class="title">文章列表</span>
      </div>
      <div class="text-box">
        <el-row>
          <el-col :span="20">
            <el-form :inline="true"
                     :model="searchKey"
                     ref="searchKey"
                     class="demo-form-inline"
                     style="width:100%">
              <el-form-item label="文章分类">
                <el-select v-model="searchKey.sid"
                           placeholder="请选择分类"
                           style="padding-left:10px"
                           size="small"
                           @focus="getArtCate">
                  <el-option v-for="item in cates"
                             :key="item.id"
                             :label="item.cate_name"
                             :value="item.cate_name">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="发布状态">
                <el-select v-model="searchKey.sst"
                           placeholder="请选择状态">
                  <el-option label="已发布"
                             value="已发布"></el-option>
                  <el-option label="草稿"
                             value="草稿"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary"
                           style="margin-left:10px;background:#4F81FF"
                           @click="onSubmit">筛选</el-button>
                <el-button @click="resetForm('searchKey')"
                           style="margin-left:10px;background:#EAECF5;color:#4F81FF">重置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="4">
            <el-button type="primary"
                       class="btn"
                       @click="openAdd"
                       style="float:right;background:#4F81FF"
                       size="small">发表文章</el-button>
          </el-col>
        </el-row>
        <el-table :data="list"
                  border
                  style="width: 100%"
                  row-key="id">
          <el-table-column label="文章标题"
                           min-width="40%"
                           prop="title">
          </el-table-column>
          <el-table-column label="分类"
                           min-width="20%"
                           prop="cate_name">
          </el-table-column>
          <el-table-column label="发表时间"
                           min-width="30%"
                           prop="pub_date">
          </el-table-column>
          <el-table-column label="状态"
                           min-width="30%"
                           prop="state">
          </el-table-column>
          <el-table-column label="操作"
                           min-width="20%">
            <template v-slot="{ row }">
              <el-button size="mini"
                         class="edit-btn"
                         @click="handleEdit(row)">修改</el-button>
              <el-button size="mini"
                         type="danger"
                         class="del-btn"
                         @click="handleDelete(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination @current-change="changePage"
                     :current-page="page.page"
                     :page-sizes="page.size"
                     :page-size="2"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="page.total">
      </el-pagination>
      <!-- @size-change="handleSizeChange" -->
    </el-card>
    <art-pub ref="artPub"
             :showdialog.sync="showdialog"
             @getArtList="getArtList"></art-pub>
  </div>
</template>

<script>
import { getArtList, getArtCate, delArtList } from '@/api/article'
import artPub from './components/art_pub'
import moment from 'moment'
export default {
  name: 'ArtCatePage',
  data () {
    return {
      list: [], // 接收分类列表数据
      loading: false,
      page: {
        page: 1,
        size: 2,
        total: 0
      },
      cates: [],
      states: [{
        value: '已发布',
        label: '已发布'
      }, {
        value: '草稿',
        label: '草稿'
      }],
      formInline: ' ',
      searchKey: {
        sid: '',
        sst: ''
      },
      showdialog: false
    }
  },
  components: {
    artPub
  },
  created () {
    this.getArtList()
  },
  methods: {
    async handleEdit (id) {
    },
    async handleDelete (id) {
      try {
        await this.$confirm('确认删除吗？')
        await delArtList(id)
        this.getArtCate()
        this.$message.success('删除成功')
        this.getArtList()
      } catch (err) {
        console.log(err)
      }
    },
    async getArtList () {
      this.loading = true
      const res = await getArtList(this.page.page, this.page.size)
      console.log('res.data.total', res.data.total)
      console.log('res.data.data', res.data.data)
      this.page.total = res.data.total
      this.list = res.data.data
      // this.list.pub_date = this.list.pub_date.toISOString()
      // this.list.pub_date = this.list.pub_date.toLocaleString()
      // this.list.pub_date = moment(res.data.data.pub_date).format('YYYY-MM-DD hh:mm:ss')
      // 处理时间格式
      this.list.forEach((item, index) => {
        for (const key in item) {
          if (key === 'pub_date') {
            item[key] = moment(item[key]).format('YYYY-MM-DD hh:mm:ss')
            console.log(item[key])
          }
        }
      })
      this.loading = false
    },
    changePage (newPage) {
      this.page.page = newPage
      this.getArtList()
    },
    async getArtCate () {
      const res = await getArtCate()
      this.cates = res.data.data
    },
    openAdd () {
      // this.$router.push('/art_pub')
      // this.$router.push({ path: '/art_pub' })
      this.showdialog = true
      alert('111')
    },
    onSubmit () {
      console.log('search', this.searchKey)
      if (this.searchKey.sid) {
        // 搜索分类条件
        // this.list = []
        // this.list.forEach((item, index) => {
        //   for (const key in item) {
        //     if (key === 'cate_name') {
        //       this.list.push(item)
        //       console.log(this.list)
        //     }
        //   }
        // })
        // return this.studentsList.filter(item =>{
        //     if(item.id == keywords) {
        //       return item
        //     }
        //   })
      }
      if (this.search.sst) {
        // 搜索状态条件
      }
    }
  }

}
</script>

<style lang="less" scoped>
.article-container {
  .box-card {
    width: 100%;

    .el-card__header {
      height: 48px;

      .title {
        font-size: 18px;
        line-height: 48px;
        font-weight: 600;
      }

      .btn {
        float: right;
        margin-top: 5px;
        font-size: 14px;
        background-color: #4f81ff;
      }
    }

    .text-box {
      padding: 10px;
      .el-table {
        font-size: 14px;
        // color: #e7e7e7;
      }
      .edit-btn {
        color: #fff;
        font-size: 14px;
        background-color: #4f81ff;
      }
      .del-btn {
        color: #fff;
        font-size: 14px;
      }
    }
  }
}
</style>
